<template>
  <div>
    <div class="search">
      <el-input v-model="powerDistName" placeholder="请输入配电箱名称" style="width:200px" clearable></el-input>
      <el-input v-model="deviceName" placeholder="请输入设备名称" style="width:200px" clearable></el-input>
      <el-input v-model="deviceId" placeholder="请输入设备编号" style="width:200px" clearable></el-input>
      <el-button class="btntable2" @click="getsbList">查&nbsp;&nbsp;&nbsp;询</el-button>
    </div>
    <el-table
      ref="singleTable"
      :data="sbList"
      class="tablecolor"
      style="width: 100%;text-align:center;margin-bottom:10px"
    >
      <el-table-column type="index" label="序列" width="100px"></el-table-column>
      <el-table-column property="powerDistName" label="配电箱名称"></el-table-column>
      <el-table-column property="deviceName" label="设备名称"></el-table-column>
      <el-table-column property="deviceId" label="设备编号"></el-table-column>
      <el-table-column label="参数" width="400">
        <template slot-scope="scope">
          <span
            v-for="(item, index) in scope.row.deviceConfiglist"
            :key="index"
          >{{item.paramCode.codeName}} 设备端口号：{{item.dataCode.codeId}} 上限：{{item.limitTop}} 下限：{{item.limitLow}}</span>
        </template>
      </el-table-column>
      <el-table-column property="updatetime" label="最后更新时间"></el-table-column>
      <el-table-column property="pdxguanli" label="管理" width="300px">
        <template slot-scope="scope">
          <!-- <el-button @click="DMbianjis(scope.row)" type="primary" size="small">编辑</el-button> -->
          <!-- <el-button @click="DMbianjis(scope.row)" type="primary" size="small">查看</el-button> -->
          <el-button class="btntable2" @click="DMbianjis(scope.row)">查&nbsp;&nbsp;&nbsp;看</el-button>
          <el-button class="btntable2" @click="DMdelete(scope.row)">删&nbsp;&nbsp;&nbsp;除</el-button>
          <!-- <el-button type="danger" size="small" @click="DMdelete(scope.row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :currentPage.sync="currentPage"
      :pageSize="pagesize"
      layout="total, prev, pager, next, jumper"
      :total="total"
      style="text-align:center;margin:20px 0"
    ></el-pagination>

    <el-dialog title="查看设备" :visible.sync="DMadd" center width="57%">
      <el-form :model="form">
        <el-form-item label="配电箱名称" label-width="100px">
          <el-input v-model="form.powerDistName" style="width:90%"></el-input>
        </el-form-item>
        <el-form-item label="设备名称" label-width="100px">
          <el-input v-model="form.deviceName" style="width:90%"></el-input>
        </el-form-item>
        <el-form-item label="设备编号" label-width="100px">
          <el-input v-model="form.deviceId" disabled style="width:90%"></el-input>
        </el-form-item>

        <div style="max-height:400px;overflow:auto;" class="scrollbar">
          <el-form-item
            label="参数"
            label-width="100px"
            v-for="(item, index) in form.deviceConfiglist"
            :key="index"
          >
            <el-select
              v-model="item.paramCode.supCode.codeId"
              placeholder="请选择"
              @change="getselect2($event,index)"
              style="width:15%"
            >
              <el-option
                v-for="item in codeList"
                :key="item.value"
                :label="item.codeName"
                :value="item.codeId"
              ></el-option>
            </el-select>
            <el-select
              v-model="item.paramCode.codeId"
              placeholder="请选择"
              style="width:15%;margin:0 20px"
            >
              <el-option
                v-for="item in item.childCode"
                :key="item.value"
                :label="item.codeName"
                :value="item.codeId"
              ></el-option>
            </el-select>端口号：
            <el-input v-model="item.dataCode.codeId" style="width:10%"></el-input>上限：
            <el-input v-model="item.limitTop" style="width:10%"></el-input>下限：
            <el-input v-model="item.limitLow" style="width:10%"></el-input>
          </el-form-item>
        </div>
      </el-form>
      <!-- <div slot="footer" class="dialog-footer">
        <el-button @click="DMadd = false">取 消</el-button>
        <el-button type="primary" @click="saveeditsb">保 存</el-button>
      </div>-->
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ts: this.newStore(),
      currentPage: 1,
      pagesize: 10,
      total: 0,
      sbList: [],
      select1: '',
      select2: '',
      codeList: [],
      codechildrenList: [],
      title: '',
      deviceId: '',
      powerDistName: '',
      deviceName: '',
      form: {},
      DMadd: false,
      data: '',
      Dataanalysis: []
    }
  },
  created() {
    this.getsbList()
    this.getcodeList()
  },
  methods: {
    saveeditsb() {
      console.log(this.form)
      var dcJson = []

      for (let i = 0; i < this.form.deviceConfiglist.length; i += 1) {
        var dcJsondata = {}
        dcJsondata.dataCodeId = this.form.deviceConfiglist[i].dataCode.codeId
        dcJsondata.paramCodeId = this.form.deviceConfiglist[i].paramCode.codeId
        dcJsondata.limitLow = this.form.deviceConfiglist[i].limitLow
        dcJsondata.limitTop = this.form.deviceConfiglist[i].limitTop

        dcJson.push(dcJsondata)
      }

      const req = {
        deviceId: this.form.deviceId,
        deviceName: this.form.deviceName,
        regionid: '',
        dcJson: JSON.stringify(dcJson),
        powerDistName: this.form.powerDistName,
        sideX: this.form.sideX,
        sideY: this.form.sideY
      }
      console.log(req)

      this.$confirm('是否保存修改数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.spost(this.ts, '/device/updatedevice', req).then(ts => {
            this.$message({
              type: 'success',
              message: '修改成功!'
            })
            this.getsbList()
            this.DMadd = false
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    getselect2(val, index) {
      console.log(val, index)
      for (let j = 0; j < this.codeList.length; j += 1) {
        if (val == this.codeList[j].codeId) {
          this.form.deviceConfiglist[index].paramCode.codeId = ''
          this.form.deviceConfiglist[index].childCode = this.codeList[
            j
          ].childCode
        }
      }
    },
    getcodeList() {
      const req = {
        sup_code_id: '01'
      }
      this.spost(this.ts, '/code/findcodebysupcodeid', req).then(ts => {
        this.codeList = ts.d
        console.log('codeList', ts.d)
      })
    },
    handleCurrentChange() {
      this.getsbList()
    },
    getsbList() {
      const req = {
        deviceId: this.deviceId,
        deviceName: this.deviceName,
        powerDistName: this.powerDistName,
        pageNum: this.currentPage - 1,
        pageSize: this.pagesize
      }
      this.spost(this.ts, '/device/selectdevicebyPage', req).then(ts => {
        // console.log(ts.d)
        this.sbList = ts.d.content
        this.total = parseInt(ts.d.totalElements)
      })
    },
    DMbianjis(item) {
      console.log(item)
      this.form = item
      for (let i = 0; i < item.deviceConfiglist.length; i += 1) {
        for (let j = 0; j < this.codeList.length; j += 1) {
          if (
            item.deviceConfiglist[i].paramCode.supCode.codeId ==
            this.codeList[j].codeId
          ) {
            item.deviceConfiglist[i].childCode = this.codeList[j].childCode
          }
        }
      }
      this.DMadd = true
    },
    DMdelete(item) {
      this.$confirm('此操作将删除该设备, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const req = {
            deviceId: item.deviceId
          }
          this.spost(this.ts, '/device/deletedevice', req).then(ts => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getsbList()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style>
.el-cascader-node:not(.is-disabled):focus, .el-cascader-node:not(.is-disabled):hover{
  background: #111c3f
}
.el-select-dropdown__item.hover, .el-select-dropdown__item:hover{
  background: #111c3f;
  color: #23ffff
}
.el-select-dropdown{
  background: #111c3f;
  color: #fff;
  border: 1px solid #0dc2ca;
}
.el-dialog__headerbtn .el-dialog__close {
  color: #eaa03b;
  font-size: 30px;
  padding-right: 50px;
}
.el-input.is-disabled .el-input__inner {
  background: rgba(8, 10, 14, 0);
  border: 1px transparent solid;
  border-image: linear-gradient(to bottom right, #2e5873, #111c3f) 1 10;
  color: #23ffff;
}
.el-input__inner {
  background: rgba(8, 10, 14, 0);
  border: 1px transparent solid;
  border-image: linear-gradient(to bottom right, #2e5873, #111c3f) 1 10;
  color: #23ffff;
}
.el-dialog--center {
  background: url('../../static/img/bigbg.png') no-repeat;
  background-size: contain;
}
.el-dialog__title {
  font-family: pmzd;
  font-size: 24px;
  color: #fff;
}
.el-pagination .btn-next,
.el-pagination .btn-prev {
  background: rgba(13, 23, 54, 0.5);
}
.el-pagination button:disabled {
  background: rgba(13, 23, 54, 0.5);
}
.el-pager li {
  background: rgba(13, 23, 54, 0.5);
  border: 1px transparent solid;
   color: #a9bfcc;
  border-image: linear-gradient(to bottom right, #2e5873, #111c3f) 1 10;
}
.el-button:focus,
.el-button:hover {
  background: url('../../static/img/btntable3.png') no-repeat;
  background-size: cover;
  border: 0;
  padding: 15px 36px;
  color: #fff;
}
.el-button {
  border: 0;
}
.el-table td,
.el-table th.is-leaf {
  border: 0;
}
.el-input__inner {
  background: rgba(8, 10, 14, 0);
  border: 1px transparent solid;
  border-image: linear-gradient(to bottom right, #2e5873, #111c3f) 1 10;
}
.el-table th,
.el-table tr {
  background: inherit;
}
.el-table--enable-row-hover .el-table__body tr:hover > td {
  background: rgba(12, 58, 108, 0.4);
}
.cell {
  text-align: center;
}
.cell div {
  text-align: center;
}
.search {
  margin-bottom: 10px;
  width: 100%;
}
.scrollbar::-webkit-scrollbar {
  /*滚动条整体样式*/
  width: 20px; /*高宽分别对应横竖滚动条的尺寸*/
  height: 20px;
}
.scrollbar::-webkit-scrollbar-thumb {
  /*滚动条里面小方块*/
  border-radius: 5px;
  background: rgba(41,237,255, 0.6);
}
.scrollbar::-webkit-scrollbar-track {
  /*滚动条里面轨道*/
  border-radius: 0;
  background: rgba(41,237,255, 0);
}
</style>

